/*
Java source file
Copyright: Umschalt.com 2012
*/
package com.david.documentation.web.view.panels;

import com.david.documentation.domain.entities.category.Category;
import com.david.documentation.service.category.CategoryService;
import com.david.documentation.web.view.components.DokuPagingNavigator;
import com.david.documentation.web.view.pages.CategoryPage;
import org.apache.wicket.Component;
import org.apache.wicket.PageParameters;
import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

public class CategoryPanel extends Panel{

	public CategoryPanel(String id,final CategoryService service) {
		super(id);

		setOutputMarkupId(true);

		DataView<Category> view = new DataView<Category>("view",new ListDataProvider(service.findAll())) {
			@Override
			protected void populateItem(Item item) {
				final Category category = (Category)item.getModelObject();
				AjaxLazyLoadPanel countPanel = new AjaxLazyLoadPanel("count") {
					@Override
					public Component getLazyLoadComponent(String markupId) {
						return new Label(markupId,"("+service.getNumberOfDokus(category.getId())+")");
					}
				};
				item.add(new BookmarkablePageLink("link", CategoryPage.class,new PageParameters("category="+category.getName()))
						.add(new Label("label",category.getName())).add(countPanel));
			}
		};
		add(view);
		view.setItemsPerPage(60);
		view.setOutputMarkupId(true);
		add(new DokuPagingNavigator("navigator", view).setVisible(view.getPageCount()>1));
		add(view);

	}
}
